package ant;



import ant.properties.Property;

import world.Position;




public class Ant extends WOAObject{

	private Colony colony;

	
	public Ant(Colony c)
	{	
		super(c.world, new Position (c.getPos().get_map(), c.getPos().getX(), c.getPos().getY()));
		
		this.setColony(c);
		// create ant in ant box
		
		type = "ant";
		c.world.log.info("ant has born.");
		

		float r = (float) (Math.random() * 5.0f);
		adn.set("size", new Property("size", r));		
		adn.set("weight", new Property("weight", 0.5f + (r / 12.0f)));
		adn.set("storage_food", new Property("storage_food", 20f + (r * 5.0f)));		
		adn.set("speed_max", new Property("speed_max", 20f + (r * 20.0f) + adn.getMax("weight") * 50));
		adn.set("acceleration", new Property("acceleration", (1 - (adn.getMax("size") * 10) / 100) * 400 + adn.getMax("speed_max")));
	
		
		//adn.put("size", new Property("size", 2));
		
		setMyBody(c.world.antBox.create_body(adn.getCurrent("size"), adn.getCurrent("weight"), c.myBody.getPosition()));
	}
	
	


	public void random_move()
	{
	}
	
	
	/**
	 * reduce physical linear velocity to speed_max
	 */
	public void limit_speed_max()
	{
		if (myBody.m_linearVelocity.x > adn.getCurrent("speed_max"))
		{
			myBody.m_linearVelocity.x = adn.getCurrent("speed_max");			
		}
		if (myBody.m_linearVelocity.y > adn.getCurrent("speed_max"))
		{
			myBody.m_linearVelocity.y = adn.getCurrent("speed_max");			
		}					
	}
	
	public void life()
	{
		
		//world.action_fifo.add(new DrawObject(id, move, "linearVelocity"));		
		//myBody.m_linearVelocity = move.getVec2();//applyImpulse(move.getVec2(), myBody.getPosition());
		//Position move_pos = new Position(pos._map, 280.0f, 140.0f);
		
		float acc = 20 * adn.getCurrent("acceleration");
		float x_acc = (float)(Math.random() * acc);
		float y_acc = (float)(Math.random() * acc);
		if (y_acc % 2 == 0) y_acc = -y_acc;
		float acc_dif = x_acc - y_acc;
		direction = myBody.getAngle();
		Position move_pos = new Position(pos._map, x_acc, y_acc);
		myBody.applyForce(move_pos.getVec2(), myBody.getPosition());
		limit_speed_max();
		
		world.log.debug(id + "; acc_dif ; " + acc_dif + "; " + myBody.getAngle() + "; " + myBody.getAngularVelocity());
		//myBody.m_linearVelocity = move_pos.getVec2();
		//world.log.debug(id + " size : " + adn.getCurrent("size") );
		//world.log.debug(id + " linear velocity : " + myBody.m_linearVelocity.x + "," + myBody.m_linearVelocity.y);

//		world.log.debug(id + " troque:" + myBody.m_torque);	
//		world.log.debug(id + " invI:" + myBody.m_invI);
//		world.log.debug(id + " m_linearDamping:" + myBody.m_linearDamping);
//		world.log.debug(id + " intertia:" + myBody);
		
		/*		
		try
		{
			Object dynamics_o =  world.woal.so.getAttribute("dynamics");		
			HashMap<Integer, DrawObject> dynamics = (HashMap<Integer, DrawObject>) dynamics_o;
			if (dynamics != null)
			{
				dynamics.put(id, getDrawObject());		
				world.woal.so.setAttribute("dynamics", dynamics);
			}
			else
			{
				world.log.debug("dynamics is null");				
			}
					
		}
		catch (Exception e)
		{
			e.printStackTrace();

		}
		*/
		//move(move_pos);
		
		/*
		if (pos.getX() > 800)
		{
			pos.setX(200);
		}
		//pos.setX(pos.getX() + 1);
		*/
/*
		EsObject message = new EsObject();
        message.setString("m_type", "ant_pos");
		message.setString("pos_x", pos.getX().toString());
        message.setString("pos_y", pos.getY().toString());       		
        this.colony.world.ESServer.getApi().sendPluginMessageToRoom(0, 0, message);
*/        

	}

	public void setColony(Colony colony) {
		this.colony = colony;
	}

	public Colony getColony() {
		return colony;
	}
}
